home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
byt85jul.lbr
/
TRAVPC1.BQS
/
TRAVPC1.BAS
Wrap
BASIC Source File
|
1985-09-15
|
8KB
|
146 lines
@set(page=0)
@style[leftmargin=11 char,rightmargin=11 char]
@style[headerspacing 3 lines,footerspacing 3 lines]
@pageheading[left="Travesty Revisited"
right="Listing, page @value(page) of 3"]
@pagefooting[center=" "]
@newpage
@begin[format] 1: ' ************************
2: ' *** TRAVESTY.BAS ***
3: ' ************************
4:
5: ' Based on the article and Pascal program "Travesty" by Hugh Kenner
6: ' and Joseph O'Rourke, in BYTE for November, 1984.
7:
8: ' Written by M. L. Lesser, November 26, 1984
9: ' Compiled with IBM PC BASIC Compiler, v 1.00, switches /N/E/O
10: ' (patches to May, 1984, have been installed)
11:
12: ' TRAVESTY scans a standard ASCII text file and generates an n-order
13: ' simulation of its letter combinations. For order n, the relation of
14: ' output to input is: "Any pattern n characters long in the output has
15: ' occurred somewhere in the input, and at about the same frequency."
16: ' If the verse flag is set, line end symbols will be replaced by "|",
17: ' which will generate line ends when they occur in the output text.
18: ' Otherwise, output lines will average 50 characters in length.
19: ' The output will be displayed during operation, and will be filed in
20: ' the standard ASCII file TRAVESTY.DOC.
21:
22: DEFINT F,I-N 'FLAG.B, FLAG.E, FLAG.V, I, K, L,
23: 'LETTER(), MAX.IN, MAX.OUT, MAX.PAT,
24: 'N.OUT, N.PAT
25: DEFSTR O-Z 'PASS, PATTERN, SOURCE, STRING,
26: 'OUT.CHAR
27: DIM LETTER(124)
28: ON ERROR GOTO 5000
29:
30: ' Default values:
31: LET MAX.IN = 30000 'Maximum input string length
32: LET MAX.PAT = 9 'Maximum scan-order length
33:
34: ' User input data:
35: RANDOMIZE 'Get randomizing seed
36: INPUT "Number of characters to be output"; MAX.OUT
37: 0100 PRINT "Scan order ( 2 - " MAX.PAT ")"; 'Simulated repeat
38: INPUT N.PAT
39: IF N.PAT < 2 OR N.PAT > 9 THEN GOTO 100 'until
40: LET N.PAT = N.PAT -1 'Convenience correction
41: 0200 INPUT "Name of input file"; SOURCE 'Error RESUME point
42: OPEN SOURCE FOR INPUT AS #1 'Trap if no file
43: INPUT "Prose or verse"; PASS
44: IF LEFT$(PASS,1) = "V" OR LEFT$(PASS,1) = "v"_
45: THEN LET FLAG.V = -1 'Set verse flag
46: ' Scan input text, deleting unwanted symbols:
47: ' (NOTE: If in verse mode, <SP>'s following line-end will be deleted)
48: PRINT
49: WHILE NOT EOF(1) 'Read input file one
50: LET PASS = INPUT$(1,#1) ' character at a time
51: IF PASS <> CHR$(13)_ 'Bug trap while
52: THEN PRINT PASS; ' displaying input
53: IF PASS = CHR$(13)_ 'Change any <CR>
54: THEN LET PASS = "" ' to <NUL>
55: IF PASS = CHR$(10)_ 'Change any <LF>
56: THEN LET PASS = " ":_ ' to <SP>
57: IF FLAG.V_ ' or (if verse)
58: THEN LET PASS = "|" ' to special line-end
59: IF PASS = CHR$(9)_ 'Change any <HT>
60: THEN LET PASS = " " ' to <SP>
61: IF PASS <> " " AND PASS <> ""_ 'Unless <SP> or <NUL>
62: THEN LET FLAG.B = 0 ' reset blank flag
63: IF NOT FLAG.B_ 'If "blank" flag clear
64: THEN LET STRING = STRING + PASS ' add to string
65: IF (FLAG.V AND PASS = "|")_ 'Set blank flag to
66: OR (PASS = " ")_ ' delete following
67: THEN LET FLAG.B = -1 ' <SP> characters
68: IF LEN(STRING) >= MAX.IN_ 'If full string:
69: THEN GOTO 300 ' break out of loop
70: WEND 'End of input loop
71: 0300 LET STRING = STRING + LEFT$(STRING,N.PAT) 'End around
72: ' Report string space usage and force garbage collection:
73: PRINT: PRINT
74: PRINT "Input string contains" LEN(STRING) "bytes"
75: PRINT "There are" FRE("") "bytes remaining in string space"
76: CLOSE #1
77: PRINT: PRINT
78: ' Open output file:
79: OPEN "TRAVESTY.DOC" FOR OUTPUT AS #2
80: ' Initial pattern:
81: LET PATTERN = LEFT$(STRING,N.PAT)
82: PRINT PATTERN;
83: PRINT #2, PATTERN;
84: LET N.OUT = N.PAT
85: 0400 'Start of major "repeat until" loop
86: ' Clear letter array (this compiler doesn't have ERASE):
87: FOR K = 0 TO 124
88: LET LETTER(K) = 0
89: NEXT K
90: ' Match current pattern:
91: LET I = INSTR(STRING,PATTERN)
92: WHILE I > 0 AND I <= LEN(STRING) - N.PAT 'Don't run off end
93: LET PASS = MID$(STRING,I+N.PAT,1) 'Next character
94: LET LETTER(0) = LETTER(0) + 1 'Update total count
95: LET K = ASC(PASS)
96: LET LETTER(K) = LETTER(K) + 1 'Update character count
97: LET I = INSTR(I+1,STRING,PATTERN) 'For next match
98: WEND 'And around again
99: ' Choose next output letter based on use frequency:
100: LET L = INT(1 + LETTER(0) * RND) 'Random choice index
101: FOR K = 32 TO 124 'Scan the letter array
102: LET L = L - LETTER(K)
103: IF L <= 0_ 'This is it
104: THEN LET OUT.CHAR = CHR$(K):_
105: GOTO 500 'Break out of loop
106: NEXT K
107: 0500 'Housekeeping for output character:
108: LET N.OUT = N.OUT + 1 'Increment count
109: IF N.OUT MOD 50 = 0_ 'If average line length
110: THEN LET FLAG.E = -1 ' set "line-end" flag
111: ' Establish next pattern:
112: LET PATTERN = MID$(PATTERN,2) + OUT.CHAR
113: ' Display and store character found:
114: IF NOT (FLAG.V AND OUT.CHAR = "|")_
115: THEN PRINT OUT.CHAR;:_
116: PRINT #2, OUT.CHAR;
117: ' Check for line break:
118: IF (FLAG.V AND OUT.CHAR = "|")_ 'Verse line end
119: OR (FLAG.E AND OUT.CHAR = " ")_ 'Force line end
120: THEN PRINT:_ ' Display <EOL>
121: PRINT #2,:_ ' File <EOL>
122: LET FLAG.E = 0:_ 'Reset forced-end flag
123: IF FLAG.V AND OUT.CHAR = " "_ 'Forced verse break
124: THEN PRINT SPACE$(5);:_ ' indents next line
125: PRINT #2, SPACE$(5);
126: IF INKEY$ = CHR$(3) THEN END 'Emergency exit
127: ' Check for end of output:
128: IF N.OUT < MAX.OUT OR OUT.CHAR <> " "_
129: THEN GOTO 400 'End of major loop
130: END
131:
132: 5000 'Error trap (on "File not found" or "Bad file name"):
133: IF ERR = 53 OR ERR = 64_
134: THEN PRINT CHR$(34) SOURCE CHR$(34) " does not exist. ";:_
135: PRINT "Try again":_
136: RESUME 200
137: ON ERROR GOTO 0
138: ' End of source code
@end[format]